package net.tinyportal.tools.spring;

import java.io.Serializable;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import net.tinyportal.javax.portlet.TpPortletPreference;
import net.tinyportal.portal.PortletHolder;
import net.tinyportal.service.PortletService;
import net.tinyportal.service.PreferenceService;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/** We need our own FactoryBean because Spring does not offer a way to
 * inject request scoped HttpServletResponse objects.
 * See http://forum.springsource.org/showthread.php?p=298179#post298179 */
public class PortletHolderFactoryBean implements FactoryBean<PortletHolder>, ApplicationContextAware, ServletContextAware, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8360550035195425285L;

	@Autowired 
	HttpServletRequest request;

	@Autowired
	ApplicationContext applicationContext;

	@Autowired
	private ServletContext servletContext;
	
	@Autowired
	PortletService portletService;
	
	@Autowired
	PreferenceService preferenceService;

	public PortletHolder getObject() throws Exception {
		PortletService portletService = applicationContext.getBean(PortletService.class);
		String portletName = null ;
		try {
			RequestAttributes requestAttributes =  RequestContextHolder.currentRequestAttributes();

			if (requestAttributes instanceof ServletRequestAttributes) {
				HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();  
				if (null != request) {
					portletName = (String)request.getAttribute("net.tinyportal.spring.portlet");
				}
			}
		} catch (IllegalStateException e) {
			//on n'est pas dans une requete web
			portletName = (String)servletContext.getAttribute("net.tinyportal.spring.portlet");
		}

		/*
		 * On recupère le nom du type de portlet
		 */
		String portletInstanceName = portletService.getPortletName(portletName);

		PortletHolder portletHolder = portletService.getPortlet(portletInstanceName).clone();
		portletHolder.setPortletName(portletName);
		portletHolder.setPortletId(Integer.toString(Math.abs(portletName.hashCode())));
		TpPortletPreference preferences =  preferenceService.getPreferenceForPortletName(portletName);
		portletHolder.setPreferences(preferences);
		
		return portletHolder;

	}

	public Class<PortletHolder> getObjectType() {
		return PortletHolder.class;
	}

	public boolean isSingleton() {
		return false;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;		
	}
	
	@Override
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

}
